﻿using System;
using Microsoft.AspNetCore.Http;
using zhangmenren.Models;
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
using System.Collections.ObjectModel;
using Microsoft.AspNetCore.Mvc;

namespace zhangmenren.Untils
{
    public static class Cookie
    {
        public static string cookieName = "zhangmenren";

        public static int expire = 7;

        public static void SaveCookie<T>(HttpContext httpContext, T t)
        {
            CookieOptions option = new CookieOptions();
            option.IsEssential = true;
            option.Expires = DateTime.Now.AddDays(expire);
            httpContext.Response.Cookies.Append(cookieName, Newtonsoft.Json.JsonConvert.SerializeObject(t), option);
        }

        public static T GetCookie<T>(HttpContext httpContext)where T :class
        {
            if (!string.IsNullOrEmpty(httpContext.Request.Cookies[cookieName]))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(httpContext.Request.Cookies[cookieName]);
            }
            return null ;

        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="httpContext"></param>
        public static void DeleteCookie(HttpContext httpContext)
        {
            httpContext.Response.Cookies.Delete(cookieName);
        }

    }


    public static class QTCookie
    {
        public static string cookieName = "zmruser";

        public static void SaveCookie<T>(HttpContext httpContext, T t)
        {
            CookieOptions option = new CookieOptions();
            option.IsEssential = true;
            httpContext.Response.Cookies.Append(cookieName, Newtonsoft.Json.JsonConvert.SerializeObject(t), option);
        }

		public static T GetCookie<T>(HttpContext httpContext) where T : class
        {
            if (!string.IsNullOrEmpty(httpContext.Request.Cookies[cookieName]))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(httpContext.Request.Cookies[cookieName]);
            }
            return null;


        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="httpContext"></param>
        public static void DeleteCookie(HttpContext httpContext)
        {
            httpContext.Response.Cookies.Delete(cookieName);
        }

    }


}
